package re56.equipment;

import re56.layer.RadioLayer;
import re56.layer.UEPhysicalLayer;
import re56.packet.PDCCHPacket;
import re56.packet.PDSCHPacket;
import re56.packet.Packet;


public class UserEquipment extends Equipment
{
	private String IMSI;
	private boolean connected;
	
	public UserEquipment(RadioLayer radioLayerDL, RadioLayer radioLayerUL)
	{
		super(radioLayerDL, radioLayerUL);
		
		Phy = new UEPhysicalLayer(Mac, radioLayerDL, radioLayerUL);
		
		radioLayerDL.useRadioLayer(this);
		radioLayerUL.useRadioLayer(this);
		
		// randomly generate a French SIM number (Orange customer).
		IMSI = "20801" + Math.abs(rand.nextInt());
		
		connected = false;
		
		
		// http://netseminar.stanford.edu/seminars/01_29_09.pdf
		// Typical Power for a Macro Cell ~23 dBm
		Phy.setPower(43);
		
		Phy.setSensitivity(-110);
	}

	@Override
	public void receive(Packet p)
	{
		if(p instanceof PDCCHPacket)
		{
			// if we received this kind of packet,
			// we are now connected.
			
			PDCCHPacket packet = (PDCCHPacket) p;
			
			if(packet.getRBs_UL().size() > 0)
			{
				connected = true;
				computeRxLevel();
			}
		}
		else if(p instanceof PDSCHPacket)
		{
			// if we received this kind of packet,
			// we are now connected.
			
			PDSCHPacket packet = (PDSCHPacket) p;
			
			System.out.println("\t\t\t\t\tUE Response Received: "+new String(packet.getData()));
		}
	}

	@Override
	public void send(Packet p, Equipment e)
	{
		if(!connected)
			return;
		
		System.out.println("\t\t\t\t\tUE "+IMSI+" just sent a packet.");
		
		super.send(p, e);
	}
	
	
	public void computeRxLevel()
	{
		// let's compute the RxLevel
		Phy.computeRxLevel();
	}
	
	
	public String getIMSI()
	{
		return IMSI;
	}
	
	
	public boolean isConnected()
	{
		return connected;
	}
}
